package ai.peanut.selfbms.controller.sys;


import ai.peanut.selfbase.common.domain.DataResult;
import ai.peanut.selfbase.common.enums.ResultTypeEnum;
import ai.peanut.selfbase.common.utils.UserUtils;
import ai.peanut.selfbase.modules.office.entity.Office;
import ai.peanut.selfbase.modules.office.service.IOfficeService;
import ai.peanut.selfbase.modules.role.service.IRoleService;
import ai.peanut.selfbase.modules.user.entity.User;
import ai.peanut.selfbase.modules.user.pojo.UserDto;
import ai.peanut.selfbase.modules.user.pojo.UserInfoDto;
import ai.peanut.selfbase.modules.user.pojo.UserParam;
import ai.peanut.selfbase.modules.user.service.IUserService;
import com.baomidou.mybatisplus.plugins.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author lichong
 * @since 2019-02-15
 */
@Api(tags="用户模块")
@Controller
@Slf4j
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService iUserService ;
    @Autowired
    private IRoleService iRoleService ;
    @Autowired
    private IOfficeService iOfficeService;

    @ApiOperation(value = "查看用户列表",httpMethod="POST")
    @RequiresPermissions("user:list")
    @RequestMapping("/list")
    @ResponseBody
    public DataResult<Page> list(UserParam params, HttpServletRequest request, HttpServletResponse response) {
        try {

            log.info("角色 管理后台 :{}", "请求参数:" + params.toString());

            Page page = iUserService.selectAllPageByParams(params);
//
            return DataResult.success(page, Page.class);
        } catch (Exception e) {
            log.error("角色 管理后台 列表异常:", e);
        }
        return DataResult.faild(ResultTypeEnum.FAILED.getCode(),
                ResultTypeEnum.FAILED.getMessage());
    }
    @ApiOperation(value = "查看用户",httpMethod="GET")

    @RequiresPermissions("user:info")
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    @ResponseBody
    public DataResult<UserDto> menuIndex(HttpServletRequest request, HttpServletResponse response){
        try {
            UserDto dto = UserUtils.getCurrentUser() ;
            return  DataResult.success( dto ,UserDto.class) ;
        }catch ( Exception e ){
            log.error("cps商品列表 管理后台 列表异常:" + e,e);
        }
        return DataResult.faild(ResultTypeEnum.FAILED.getCode(),
                ResultTypeEnum.FAILED.getMessage());
    }
    @RequiresPermissions("user:password")
    @RequestMapping(value = "/password", method = RequestMethod.POST)
    @ResponseBody
    public DataResult menuIndex(@RequestBody UserParam userParam,HttpServletRequest request, HttpServletResponse response){
        try {
            return iUserService.updatePassword(userParam);
        }catch ( Exception e ){
            log.error("cps商品列表 管理后台 列表异常:" + e,e);
        }
        return DataResult.faild(ResultTypeEnum.FAILED.getCode(),
                ResultTypeEnum.FAILED.getMessage());
    }
    @RequiresPermissions("user:getInfoById")
    @RequestMapping(value = "/getInfoById", method = RequestMethod.GET)
    @ResponseBody
    public DataResult<UserInfoDto> getInfoById(Long id ,HttpServletRequest request, HttpServletResponse response){
        try {
            User user = iUserService.selectById(id);
            UserInfoDto infoDto = new UserInfoDto() ;
            if(user == null ) {
                return  DataResult.success( infoDto ,UserInfoDto.class) ;
            }
            BeanUtils.copyProperties(user,infoDto);
            infoDto.setPassword(null);
            infoDto.setRoleIdList(iRoleService.selectRoleIdListByUserId(id));
            //officeName
            Office office = iOfficeService.selectById(user.getOfficeId());
            if(office != null ) {
                infoDto.setOfficeName(office.getName());
            }
            return  DataResult.success( infoDto ,UserInfoDto.class) ;
        }catch ( Exception e ){
            log.error("cps商品列表 管理后台 列表异常:" + e,e);
        }
        return DataResult.faild(ResultTypeEnum.FAILED.getCode(),
                ResultTypeEnum.FAILED.getMessage());
    }
    @RequiresPermissions("user:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public DataResult<Boolean> saveOrUpdate(@RequestBody UserParam params, HttpServletRequest request, HttpServletResponse response) {
        try {
            if (params.getId() == null) {
                return iUserService.insertUser(params);
            } else {
                return iUserService.updateUser(params);
            }
        } catch (Exception e) {
            log.error("cps商品修改 管理后台 :", e);
        }
        return DataResult.faild(ResultTypeEnum.FAILED.getCode(),
                ResultTypeEnum.FAILED.getMessage());
    }
    @RequiresPermissions("user:delete")
    @RequestMapping(value ="/delete", method = RequestMethod.POST)
    @ResponseBody
    public DataResult<Boolean> delete(@RequestBody UserParam params) {
        try {
            log.info("cps商品删除 管理后台 :{}", "请求参数:" + params.toString());
            params.setUpdateUserId(UserUtils.getCurrentUser().getId());
            params.setDelFlag(0);
            int deNum = iUserService.deleteByIds(params);
            return DataResult.success();
        } catch (Exception e) {
            log.error("cps商品删除 管理后台 :", e);
        }
        return DataResult.faild(ResultTypeEnum.FAILED.getCode(),
                ResultTypeEnum.FAILED.getMessage());

    }
}

